import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { FlowersModule } from "../../../modules/FlowersModule";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { IFlowersModule } from "../../../modules/interfaces/IFlowersModule";
import { IFlowersBlueModule } from "../../../modules/interfaces/IFlowersBlueModule";
import { DataEvent } from "../events/DataEvent";
import { ItemData } from "../resource/info/ItemData";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SHandselFlowerBack } from "../../../Message/Public/SHandselFlowerBack";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { PlayerMiniInfo } from "../model/PlayerMiniInfo";
import { FlowersData } from "../view/flowers/FlowersData";
import { GameProxy } from "../mvc/GameProxy";
import { EHandselFlowerType } from "../../../Message/Game/EHandselFlowerType";
import { Alert } from "../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { Language } from "../../../extend/language/Language";
import { FlowersDataCross } from "../view/flowers/FlowersDataCross";
import { PlayerUtil } from "../view/common/util/PlayerUtil";
import { SEntityId } from "../../../Message/Public/SEntityId";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { ECopyType } from "../../../Message/Public/ECopyType";
import { LayerManager } from "../manager/LayerManager";
import { SReceiveFlowerInfo } from "../../../Message/Game/SReceiveFlowerInfo";
import { EntityUtil } from "../scene/layer/utils/EntityUtil";
import { SFloatFlower } from "../../../Message/Game/SFloatFlower";
import { SystemSetter } from "../view/system/SystemSetter";
import { FlowerType } from "../../../extend/flash/flower/FlowerType";
import { ResFileConst } from "../resource/ResFileConst";
import { FlowerManager } from "../../../extend/flash/flower/FlowerManager";
import { FlowerNewModule } from "../../../modules/FlowerNewModule";
import { FlowersBlackModule } from "../../../modules/FlowersBlackModule";
import { FlowersBlueModule } from "../../../modules/FlowersBlueModule";
import { _EHandselFlowerTypeAnonymous } from "../../../EHandselFlowerType/_EHandselFlowerTypeAnonymous";
type int = number;
//class FlowersController
    
    export  class FlowersController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (!this._flowersModule) 
            {
                this._flowersModule = new FlowersModule();
            }
            return this._flowersModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.FlowerOpenHandselFlower, this.onOpenHandselFlowers);
            Dispatcher.addEventListener(EventName.FlowerUseInPack, this.onFlowerUseInPack);
            Dispatcher.addEventListener(EventName.FlowerHandselFlower, this.onFlowerHandselFlower);
            Dispatcher.addEventListener(EventName.FlowerKissBack, this.onFlowerKissBack);
            Dispatcher.addEventListener(EventName.FlowerHandselFlowerCross, this.onFlowerHandselFlowerCross);
            Dispatcher.addEventListener(EventName.FlowerKissBackCross, this.onFlowerKissBackCross);
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            NetDispatcher.addCmdListener(ServerCommand.FlowersReceiveFlower, this.onReceiveFlower);
            NetDispatcher.addCmdListener(ServerCommand.FlowersWreathEffect, this.onWreathEffect);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateHandselFlowerBack, this.onHandselFlowerBack);
            Dispatcher.addEventListener(EventName.FlowerBuleOpenHandselFlower, this.onFlowerBuleOpenHandselFlower);
            NetDispatcher.addCmdListener(ServerCommand.FriendOnlineStatus, this.onFriendOnlienStatus);
            return;
        }

        /* internal  */onFriendOnlienStatus(arg1: Object): void
        {
            if (!view.isHide && (view as IFlowersModule).isMainPanel) 
            {
                (view as IFlowersModule).updateFriendList();
            }
            if (!this.blueView.isHide && (this.blueView as IFlowersBlueModule).isMainPanel) 
            {
                (this.blueView as IFlowersBlueModule).updateFriendList();
            }
            if (!this.blackView.isHide && (this.blackView as IFlowersBlueModule).isMainPanel) 
            {
                (this.blackView as IFlowersBlueModule).updateFriendList();
            }
            if (!this.newView.isHide && (this.newView as IFlowersBlueModule).isMainPanel) 
            {
                (this.newView as IFlowersBlueModule).updateFriendList();
            }
            return;
        }

        /* internal  */onFlowerBuleOpenHandselFlower(arg1: DataEvent): void
        {
            var loc1=arg1.data.name as string;
            var loc2=arg1.data.idata as ItemData;
            if (loc2.itemInfo.item.level != 3) 
            {
                if (loc2.itemInfo.item.level != 2) 
                {
                    if (loc2.itemInfo.item.level == 4) 
                    {
                        this.newView.show();
                        this.newView.propItemData = loc2;
                        this.newView.showHandselFlowerPanel();
                        this.newView.setDefaultName(loc1);
                    }
                }
                else 
                {
                    this.blueView.show();
                    this.blueView.propItemData = loc2;
                    this.blueView.showHandselFlowerPanel();
                    this.blueView.setDefaultName(loc1);
                }
            }
            else 
            {
                this.blackView.show();
                this.blackView.propItemData = loc2;
                this.blackView.showHandselFlowerPanel();
                this.blackView.setDefaultName(loc1);
            }
            return;
        }

        /* internal  */onHandselFlowerBack(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SHandselFlowerBack;
            var loc2=new ItemData(loc1.code);
            if (loc2 && loc2.category == ECategory._ECategoryProp && loc2.type == EProp._EPropFlower) 
            {
                if (loc2.itemInfo.item.level != 4) 
                {
                    if (loc2.itemInfo.item.level != 1) 
                    {
                        if (loc2.itemInfo.item.level != 2) 
                        {
                            this.blackView.show();
                            this.blackView.showSendFolwerSuccess(loc1);
                        }
                        else 
                        {
                            this.blueView.show();
                            this.blueView.showSendFolwerSuccess(loc1);
                        }
                    }
                    else 
                    {
                        view.show();
                        (view as IFlowersModule).showSendFolwerSuccess(loc1);
                    }
                }
                else 
                {
                    this.newView.show();
                    this.newView.showSendFolwerSuccess(loc1);
                }
            }
            return;
        }

        /* internal  */onOpenHandselFlowers(arg1: DataEvent): void
        {
            var loc3=null;
            var loc1="";
            var loc2=-1;
            if (arg1.data) 
            {
                if (typeof arg1.data === "string") 
                {
                    loc1 = arg1.data as string;
                }
                else if (typeof arg1.data === "playerminiinfo") 
                {
                    loc1 = (loc3 = arg1.data as PlayerMiniInfo).playerName;
                }
                else if (typeof arg1.data === "object") 
                {
                    loc1 = arg1.data.name as string;
                    loc2 = arg1.data.num as int;
                    loc3 = arg1.data.toPlayer;
                }
            }
            view.show();
            (view as IFlowersModule).showHandselFlowerPanel();
            (view as IFlowersModule).setDefaultName(loc1);
            if (loc3) 
            {
                (view as IFlowersModule).setPlayerMiniInfo(loc3);
            }
            if (loc2 != -1) 
            {
                (view as IFlowersModule).setSelectItem(loc2);
            }
            return;
        }

        /* internal  */goHandselFlower(arg1: FlowersData): void
        {
            GameProxy.flowers.handselFlower(arg1.playerName, arg1.isSign ? EHandselFlowerType._EHandselFlowerTypeSign : _EHandselFlowerTypeAnonymous, arg1.propUid, arg1.autoBuy, arg1.flowerCode);
            view.hide();
            return;
        }

        /* internal  */onFlowerHandselFlower(arg1: DataEvent): void
        {
            var e: DataEvent;
            var flowersData: FlowersData;
            var onClose: Function;
            var playerinfo: PlayerMiniInfo;
            var isNeedAlert: Boolean;

            var loc1;
            flowersData = null;
            onClose = null;
            playerinfo = null;
            isNeedAlert = false;
            e = arg1;
            onClose = (arg1: int, arg2: Boolean): void
            {
                if (arg1 != Alert.OK) 
                {
                    if (arg1 == Alert.YES) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.FriendRequestAddFriend, {"name": flowersData.playerName}));
                    }
                }
                else 
                {
                    goHandselFlower(flowersData);
                }
                if (arg2) 
                {
                    PulseSharedObject.save("SendFlowerADDFriend", true);
                }
                return;
            }
            flowersData = e.data as FlowersData;
            if (cache.friend.isFriend(flowersData.playerName)) 
            {
                this.goHandselFlower(flowersData);
            }
            else 
            {
                playerinfo = FlowersModule(view).playerMiniInfo;
                isNeedAlert = !PulseSharedObject.isTodayNotTips("SendFlowerADDFriend");
                if (isNeedAlert) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.yesLabel = Language.getString(41418);
                    Alert.show(Language.getStringByParam(41400, flowersData.playerName), null, Alert.OK | Alert.YES | Alert.CANCEL, null, onClose);
                }
                else 
                {
                    this.goHandselFlower(flowersData);
                }
            }
            return;
        }

        /* internal  */onFlowerHandselFlowerCross(arg1: DataEvent): void
        {
            var e: DataEvent;
            var flowersDataCross: FlowersDataCross;
            var onClose: Function;

            var loc1;
            flowersDataCross = null;
            onClose = null;
            e = arg1;
            onClose = (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    GameProxy.flowers.handselFlowerCross(flowersDataCross.miniPlayer.entityId, flowersDataCross.isSign ? EHandselFlowerType._EHandselFlowerTypeSign : _EHandselFlowerTypeAnonymous, flowersDataCross.propUid, flowersDataCross.autoBuy, flowersDataCross.flowerCode);
                    view.hide();
                }
                return;
            }
            flowersDataCross = e.data as FlowersDataCross;
            Alert.show(Language.getStringByParam(41415, PlayerUtil.getProxyAndName(flowersDataCross.miniPlayer)), null, Alert.OK | Alert.CANCEL, null, onClose);
            return;
        }

        /* internal  */onFlowerKissBack(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.flowers.flowerKissBack(loc1);
            return;
        }

        /* internal  */onFlowerKissBackCross(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEntityId;
            GameProxy.flowers.flowerKissBackCross(loc1);
            return;
        }

        public isInShieldFlowerScene(): Boolean
        {
            var loc1=GameMapUtil.curMapState.isArenaCross || GameMapUtil.curMapState.isArena || GameMapUtil.curMapState.isWrestle || GameMapUtil.curMapState.isGangFights || GameMapUtil.curMapState.isNewBattlefield || GameMapUtil.curMapState.isCrossBoss || GameMapUtil.curMapState.isGuildElite || GameMapUtil.curMapState.isCrossSpiritWarCopyCode || GameMapUtil.curMapState.isLadderBattlefield || GameMapUtil.curMapState.isDominionCopy || cache.copy.isInCopyType(ECopyType._ECopyCrossStair);
            return loc1;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            if (this.isInShieldFlowerScene()) 
            {
                LayerManager.flowersLayer.visible = false;
                LayerManager.newFlowersLayer.visible = false;
            }
            else 
            {
                LayerManager.flowersLayer.visible = true;
                LayerManager.newFlowersLayer.visible = true;
            }
            return;
        }

        /* internal  */onFlowerUseInPack(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1.itemInfo.item.level != 4) 
            {
                if (loc1.itemInfo.item.level != 3) 
                {
                    if (loc1.itemInfo.item.level != 2) 
                    {
                        view.show();
                        (view as IFlowersModule).showHandselFlowerPanel();
                        (view as IFlowersModule).setSelectItem(loc1.effect);
                    }
                    else 
                    {
                        this.blueView.show();
                        this.blueView.propItemData = loc1;
                        this.blueView.showHandselFlowerPanel();
                    }
                }
                else 
                {
                    this.blackView.show();
                    this.blackView.propItemData = loc1;
                    this.blackView.showHandselFlowerPanel();
                }
            }
            else 
            {
                this.newView.show();
                this.newView.propItemData = loc1;
                this.newView.showHandselFlowerPanel();
            }
            return;
        }

        /* internal  */onReceiveFlower(arg1: SReceiveFlowerInfo): void
        {
            var loc1=new ItemData(arg1.itemCode);
            if (this.isInShieldFlowerScene()) 
            {
                return;
            }
            if (!EntityUtil.equal(arg1.miniPlayer.entityId, cache.role.entityInfo.entityId)) 
            {
                if (loc1.itemInfo.item.level != 4) 
                {
                    if (loc1.itemInfo.item.level != 1) 
                    {
                        if (loc1.itemInfo.item.level != 2) 
                        {
                            this.blackView.show();
                            this.blackView.showFlowerKissBackPanel(arg1);
                        }
                        else 
                        {
                            this.blueView.show();
                            this.blueView.showFlowerKissBackPanel(arg1);
                        }
                    }
                    else 
                    {
                        view.show();
                        (view as IFlowersModule).showFlowerKissBackPanel(arg1);
                    }
                }
                else 
                {
                    this.newView.show();
                    this.newView.showFlowerKissBackPanel(arg1);
                }
            }
            return;
        }

        /* internal  */onWreathEffect(arg1: SFloatFlower): void
        {
            if (SystemSetter.currentSetter.hideLightAndEffect) 
            {
                return;
            }
            var loc1=new ItemData(arg1.itemCode);
            var loc2=loc1.itemInfo.item.effect;
            var loc3=loc1.itemInfo.item.level;
            var loc4=FlowerType.FLOWER_9;
            var loc5=10000;
            var loc6=ResFileConst.flowers;
            if (loc3 != 1) 
            {
                if (loc3 != 2) 
                {
                    if (loc3 != 3) 
                    {
                        if (loc3 == 4) 
                        {
                            loc6 = ResFileConst.flowersNew;
                            if (loc2 >= 999) 
                            {
                                loc4 = FlowerType.FLOWERNEW_999;
                            }
                        }
                    }
                    else 
                    {
                        loc6 = ResFileConst.blackFlowers;
                        if (loc2 < 200) 
                        {
                            loc4 = FlowerType.FLOWERBLACK_99;
                        }
                        else if (loc2 >= 999) 
                        {
                            loc4 = FlowerType.FLOWERBLACK_999;
                        }
                    }
                }
                else if (loc2 < 200) 
                {
                    loc4 = FlowerType.FLOWERBLUE_99;
                }
                else if (loc2 >= 999) 
                {
                    loc4 = FlowerType.FLOWERBLUE_999;
                }
            }
            else if (loc2 == 99 || loc2 == 9) 
            {
                loc4 = FlowerType.FLOWER_99;
            }
            else if (loc2 == 999) 
            {
                loc4 = FlowerType.FLOWER_999;
            }
            if (loc2 <= 200) 
            {
                loc5 = 10000;
            }
            else if (loc2 >= 999) 
            {
                loc5 = 15000;
                if (loc4 == FlowerType.FLOWERNEW_999 || loc4 == FlowerType.FLOWERNEWBLUE_999) 
                {
                    loc5 = 30000;
                }
            }
            FlowerManager.instance.addFlowerQueue(loc4, loc5, loc6);
            return;
        }

        public get newView(): IFlowersBlueModule
        {
            if (!this._flowersNewModule) 
            {
                this._flowersNewModule = new FlowerNewModule();
            }
            return this._flowersNewModule;
        }

        public get blackView(): IFlowersBlueModule
        {
            if (!this._flowersBlackModule) 
            {
                this._flowersBlackModule = new FlowersBlackModule();
            }
            return this._flowersBlackModule;
        }

        public get blueView(): IFlowersBlueModule
        {
            if (!this._flowersBlueModule) 
            {
                this._flowersBlueModule = new FlowersBlueModule();
            }
            return this._flowersBlueModule;
        }

        private /* var */_flowersModule: IFlowersModule;

        private /* var */_flowersNewModule: FlowerNewModule;

        private /* var */_flowersBlackModule: FlowersBlackModule;

        private /* var */_flowersBlueModule: FlowersBlueModule;
    }
